package timing.ukulele.portal.mapper;

import timing.ukulele.persistent.mapper.BaseMapper;
import timing.ukulele.portal.persistent.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {

    @Insert("INSERT INTO r_user_system (user_id, system_id) VALUES (#{userId}, #{systemId})")
    int saveUserStytem(@Param("userId") Long userId, @Param("systemId") Long systemId);

    @Select("SELECT system_id FROM r_user_system WHERE user_id = #{userId}")
    List<Long> getSystemIdsByUserId(@Param("userId") Long userId);

    @Delete("<script>" +
            "DELETE FROM r_user_system Where user_id = #{userId} AND system_id in" +
            "<foreach collection=\"systemIds\" item=\"systemId\" open=\"(\" close=\")\" separator=\",\">#{systemId}</foreach>" +
            "</script>")
    int removeSystemIdsByRole(@Param("userId") Long userId, @Param("systemIds") List<Long> systemIds);

    @Select("<script>" +
            "SELECT user_id FROM r_user_system WHERE system_id in" +
            "<foreach collection=\"systemIds\" item=\"systemId\" open=\"(\" close=\")\" separator=\",\">#{systemId}</foreach>" +
            "</script>")
    List<Long> getUserIdsBySystemIds(@Param("systemIds") List<Long> systemIds);

    @Delete("DELETE FROM r_user_system Where user_id = #{userId}")
    int removeSystemIdsByUserId(@Param("userId") Long userId);

}
